package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dto.DishDto;
import com.chen.entity.Dish;
import com.chen.entity.DishFlavor;
import com.chen.mapper.DishMapper;
import com.chen.service.DishFlavorService;
import com.chen.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        //判断口味是否为空
        if(flavors.size() > 0 ){
            flavors = flavors.stream().map((item) -> {
                item.setDishId(dishId);
                return item;
            }).collect(Collectors.toList());

            //保存菜品口味数据到菜品口味表dish_flavor
            dishFlavorService.saveBatch(flavors);
        }

    }

    /**
     * 修改菜品信息和口味
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //修改菜品信息
        this.updateById(dishDto);
        Long dishId = dishDto.getId();
        //查询之前的口味
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishId);
        List<DishFlavor> list = dishFlavorService.list(lqw);
        //获得修改后的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //筛选出 这次有的  之前也有的 移除集合
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < flavors.size(); j++) {
                if (list.get(i).getId().equals(flavors.get(j).getId()) ){
                    list.remove(i);
                    i=-1;
                    break;
                }
            }
        }
        //将之前有的口味，这次没有的逻辑删除 ，再将更改的口味添加  如果没有直接添加更改的口味
        if(list.size() > 0){
            for (DishFlavor dishFlavor : list) {
                dishFlavorService.removeById(dishFlavor.getId());
            }
        }
        if(flavors.size() > 0 ){
            //遍历将菜品id设置进去
            flavors = flavors.stream().map(item -> {
                item.setDishId(dishId);
                return item;
            }).collect(Collectors.toList());

            //保存到数据库中
            for (DishFlavor flavor : flavors) {
                //如果口味id为null 则是新添加的口味，如果不为null 则口味已存在
                if (flavor.getId() == null){
                    dishFlavorService.save(flavor);
                }
            }
        }
    }
}